package com.hsxc.cdz.monitor.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hsxc.cdz.base.util.Constants;
import com.hsxc.cdz.mgmt.GlobalManager;
import com.hsxc.cdz.monitor.bean.MessageNew;
import com.hsxc.cdz.monitor.bean.pile.v1_21.RegisterPileMsg;
import com.hsxc.cdz.monitor.executor.PileExecutor;
import com.hsxc.cdz.monitor.executor.PileExecutorNew;

import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;

/**
 * 00交流 01直流
 * 
 * @author 陈钊
 *
 */
@Component
@Sharable
public class LoadInHandler extends ChannelInboundHandlerAdapter {

	@Autowired
	private PileExecutor pileExecutor;
	@Autowired
	private PileExecutorNew pileExecuterNew;

	private Logger logger = LoggerFactory.getLogger(LoadInHandler.class);
	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		MessageNew message = (MessageNew) msg;
		// 判断充电桩是否注册
		if (message.getIsRegister().equals(Constants.NO_REGISTER)
				&& message.getFrame().equals(Constants.CLIENT_SEND_REGISTER)) {
			// 1,未注册的电桩，进行注册
			logger.info("充电桩进行注册");
			if (message.getPileType() == 1 || message.getProtocolVersion().equals("0133")) {
				// 直流桩
				com.hsxc.cdz.monitor.bean.pile.v1_33.RegisterPileMsg registerPileMsg = new com.hsxc.cdz.monitor.bean.pile.v1_33.RegisterPileMsg();
				registerPileMsg.getContentFromByte(message.getContent());
				pileExecuterNew.register_zhiliu(message.getAddress(), message.getOperator(), registerPileMsg,
						message.getLineNum(), message.getProtocolVersion(), message.getPileType(), ctx);
			} else {
				// 交流桩
				RegisterPileMsg registerMsg = new RegisterPileMsg();
				registerMsg.getContentFromByte(message.getContent());
				pileExecutor.register_jiaoliu(message.getAddress(), message.getOperator(), registerMsg,
						message.getLineNum(), message.getProtocolVersion(), message.getPileType(), ctx);
			}

		} else if (message.getIsRegister().equals(Constants.DID_REGISTER)) {
			// 2,已注册，未登录的电桩，进行登录
			if (message.getFrame().equals(Constants.CLIENT_SEND_REGISTER)) {
				logger.info("充电桩进行登录");

				if (message.getPileType() == 1 || message.getProtocolVersion().equals("0133")) {
					com.hsxc.cdz.monitor.bean.pile.v1_33.RegisterPileMsg registerPileMsg = new com.hsxc.cdz.monitor.bean.pile.v1_33.RegisterPileMsg();
					registerPileMsg.getContentFromByte(message.getContent());
					pileExecuterNew.load(message.getAddress(), message.getOperator(), registerPileMsg,
							message.getLineNum(), message.getPileType(), message.getProtocolVersion(), ctx);
				} else {

					RegisterPileMsg registerContent = new RegisterPileMsg();
					registerContent.getContentFromByte(message.getContent());
					pileExecutor.load(message.getAddress(), message.getOperator(), registerContent,
							message.getLineNum(), message.getPileType(), message.getProtocolVersion(), ctx);
				}
			}
			// 3,已注册，已登录的电桩，进行消息处理
			else {
				if (GlobalManager.pileManager.containPile(message.getAddress())) {
					// 数据传入下一个handler
					ctx.fireChannelRead(msg);
					return;
				}
			}
		}
		ReferenceCountUtil.release(msg);
	}

}
